package com.dj.mingkong.admin.biz.controller.system;


import com.dj.mingkong.admin.biz.controller.system.vo.request.LoginVo;
import com.dj.mingkong.admin.biz.controller.system.vo.request.UserQueryVo;
import com.dj.mingkong.admin.biz.controller.system.vo.request.UserUpdateVo;
import com.dj.mingkong.admin.biz.controller.system.vo.response.UserInfoVo;
import com.dj.mingkong.admin.biz.controller.system.vo.response.UserVo;
import com.dj.mingkong.admin.biz.service.system.UserService;
import com.dj.mingkong.common.annotation.HasRoles;
import com.dj.mingkong.common.enums.RoleEnum;
import com.dj.mingkong.common.infrastructure.base.dto.delete.DeleteVo;
import com.dj.mingkong.common.infrastructure.base.dto.page.PageVo;
import com.dj.mingkong.common.infrastructure.base.dto.rest.RestResponse;
import com.dj.mingkong.common.util.AssertUtils;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

/**
 * 用户controller
 *
 * @author zenglonghui
 * @date: 2022/5/23 11:20
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    /**
     * 用户登录
     *
     * @param loginVo 实例对象
     * @return 是否成功，true-成功，false-失败
     * @throws RuntimeException loginVo为空的时候会抛出运行时异常
     */
    @PostMapping("/login")
    public RestResponse<String> login(HttpServletRequest request, @RequestBody @Validated LoginVo loginVo) {
        return RestResponse.success(userService.login(request, loginVo));
    }

    /**
     * 获取验证码图片
     *
     * @param request  请求
     * @param response 返回
     */
    @GetMapping("/getVerifyCodeImage")
    public void getVerifyCodeImg(HttpServletRequest request, HttpServletResponse response) {
        userService.getVerifyCodeImage(request, response);
    }

    /**
     * 获取登录用户的详情信息
     *
     * @return UserInfoVo
     */
    @GetMapping("info")
    public RestResponse<UserInfoVo> info() {
        return RestResponse.success(userService.info());
    }

    /**
     * 修改数据
     *
     * @param updateVo 实例对象
     * @return 是否成功，true-成功，false-失败
     * @throws RuntimeException userVo.getId()为空的时候会抛出运行时异常
     */
    @PostMapping("/updateById")
    @HasRoles(RoleEnum.ADMIN)
    public RestResponse<Boolean> updateById(@RequestBody @Validated UserUpdateVo updateVo) {
        userService.updateById(updateVo);
        return RestResponse.buildSuccess();
    }

    /**
     * 通过主键删除数据
     *
     * @param deleteVo -id 主键
     * @return 是否成功，true-成功，false-失败
     * @throws RuntimeException id 为空的时候会抛出运行时异常
     */
    @PostMapping("/deleteById")
    @HasRoles(RoleEnum.ADMIN)
    public RestResponse<Boolean> deleteById(@RequestBody @Validated DeleteVo deleteVo) {
        userService.deleteById(deleteVo);
        return RestResponse.buildSuccess();
    }

    /**
     * 查询多条数据
     *
     * @param queryVo 查询Vo queryVo - pageNum 查询第几页，如果输入是 < 1 那么会被重置成1 queryVo - pageSize
     *                查询分页大小，如果输入是 < 1 那么会被重置成1
     * @return 对象分页
     * @throws RuntimeException queryVo为空的时候会抛出运行时异常
     */
    @PostMapping("/pageByVo")
    @HasRoles(RoleEnum.ADMIN)
    public RestResponse<PageVo<UserVo>> pageByVo(@RequestBody UserQueryVo queryVo) {
        AssertUtils.checkQueryPageVo(queryVo);
        return RestResponse.success(userService.pageByVo(queryVo));
    }

}
